/*******************************************************************************
# mysql -h 172.24.16.16 -P 13306 -uroot -p

grant select,insert,update,delete,create,index on *.* to user1@'%' identified by '4aKadedr';
flush privileges;

# mysql -h 172.24.16.16 -P 13306 -u user1 -p

CREATE DATABASE db1;
USE db1;
CREATE TABLE IF NOT EXISTS `UserTab`(
   `UserId` INT UNSIGNED AUTO_INCREMENT,
   `Username` VARCHAR(100) NOT NULL,
   `Password` VARCHAR(40) NOT NULL,
   `Email` VARCHAR(100) NOT NULL,
   `Hiredate` DATE, 
   `Regtime` DATETIME,
   PRIMARY KEY (`UserId`),
   UNIQUE KEY `Username` (`Username`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
*******************************************************************************/

package main

import (
	"net/http"
	"jinquan.supor.com/echos"
	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	"jinquan.supor.com/samples/remotecall/cb"
)

/*
 测试方法 ->>
 curl -X POST http://k8s.supor.com:11323/ApiNewRegist -d 'Username=ljquan;Password=abc1234'
 curl -X POST http://k8s.supor.com:11323/ApiUserAuth -d 'Username=ljquan;Password=abc1234'
 curl k8s.supor.com:11323/api0 -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibGpxdWFuIiwiZXhwIjoxNTgyMzAzMDQwfQ.UJ02SRdosjr0A4aG3vdx_QriWw4wQzqwirKjlxe9oOk"
 curl -X POST k8s.supor.com:11323/shell -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoibGpxdWFuIiwiZXhwIjoxNTg1Njc0MzQ1fQ.QVdMCI4JAcr4fwE3fPStjuSv0lPvBZRPdIDg-T5TmQs" -d 'args=123456'
 因为使用数据库中的用户表做身份验证, curl http Header中携带的用户+密码信息必须真实有效
 */
func HttpApi_Common(e *echo.Echo) {
	//http API, 用户验证与回调函数注册; post路由
	e.POST("/ApiUserAuth",   echos.API_UserAuth) 
	e.POST("/ApiNewRegist",  echos.API_NewRegist)
	e.POST("/ApiUserChPass", echos.API_UserChPass)
	e.POST("/ApiUserDel", 	 echos.API_UserDel)

}

func HttpApi_Restrict(e *echo.Echo) {
	echos.Restrict_GetCb_Registry("/api0", e, echos.API_CbFunc_0)
	echos.Restrict_GetCb_Registry("/api1", e, echos.API_CbFunc_1)
	echos.Restrict_GetCb_Registry("/api2", e, echos.API_CbFunc_2)
	echos.Restrict_PostCb_Registry("/shell", e, myapi.API_CbFunc_shellSort)
}

func accessible(c echo.Context) error {
	return c.String(http.StatusOK, "healthy")
}

func main() {
	echos.MySQL_DbLink_Construct("user1", "4aKadedr", "tcp",
								 "172.24.16.16", 13306, "db1")

	//assets := Assets_Directory()
	e := echo.New()
	// Middleware
	e.Use(middleware.Logger())
	e.Use(middleware.Recover())
	
	HttpApi_Common(e)  //远程普通API：不需要验证，直接调用
	HttpApi_Restrict(e) //远程安全API：需要验证才能调用

	e.GET("/healthy", accessible) //健康检查页面
	
	e.Logger.Fatal(e.Start(":1323"))
	
}
